/* Quiz Manager - Create and edit web quizzes.
 * Copyright (C) 2008  Alexis Brunet, Jean-Michel Brunet, Enrique Hotes, Carlo Russo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package soen343.domain;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.dsrg.ts.threadLocal.DbRegistry;

import soen343.data.MultipleChoiceQuestionTDG;
import soen343.data.NumericQuestionTDG;
import soen343.data.QuestionTDG;

public class QuestionMapper {
	private QuestionMapper() {}

	public static Question findById(int id) {
		Question question = null;
		
		try {
			ResultSet rs = QuestionTDG.findById(id);
			rs.first();
			
			switch( rs.getInt("type") ) {
				case QuestionFactory.SHORT_ANSWER:
					question = createShortAnswerQuestion(rs);
					break;
				case QuestionFactory.NUMERIC:
					question = createNumericQuestion(rs);
					break;
				case QuestionFactory.MULTIPLE_CHOICE:
					question = createMultipleChoiceQuestion(rs);
					break;
			}
		} catch(SQLException e) {
			// just return the null object
		}
		
		return question;
	}

	private static Question createMultipleChoiceQuestion(ResultSet rs) throws SQLException {
		rs.first();
		MultipleChoiceQuestion question = new MultipleChoiceQuestion(
				rs.getInt("quizid"),
				rs.getString("name"),
				rs.getInt("id"));
		question.setText(rs.getString("text"));		

		ResultSet answerRS = MultipleChoiceQuestionTDG.findById(rs.getInt("id"));		
		while(answerRS.next()) {
			question.addChoice(answerRS.getString("text"));
		}
		return question;
	}

	private static Question createNumericQuestion(ResultSet rs) throws SQLException {
		rs.first();
				
		NumericQuestion question = new NumericQuestion(
				rs.getInt("quizid"),
				rs.getString("name"),
				rs.getInt("id"));
		question.setText(rs.getString("text"));
		
		ResultSet answerRS = NumericQuestionTDG.findById(rs.getInt("id"));
		answerRS.first();
		
		question.setBounds(
				answerRS.getDouble("upperbound"),
				answerRS.getDouble("lowerbound"));
		return question;
	}

	private static Question createShortAnswerQuestion(ResultSet rs) throws SQLException {
		rs.first();
		ShortAnswerQuestion question = new ShortAnswerQuestion(
				rs.getInt("quizid"),
				rs.getString("name"),
				rs.getInt("id"));
		question.setText(rs.getString("text"));
		
		return question;
	}
	
	public static void closeConnection() throws SQLException {
		DbRegistry.closeDbConnection();
	}

	public static void delete(Question q) throws SQLException {
		QuestionTDG.delete(q.getId());		
	}
}
